package com.tanhua.server.controller;

import com.tanhua.model.vo.PageVo;
import com.tanhua.model.vo.ReportVo;
import com.tanhua.server.serivce.TestSoulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/testSoul")
public class TestSoulController {

    @Autowired
    private TestSoulService testSoulService;

    /**
     * 问卷列表的展示
     *  GET  /testSoul
     */
    @GetMapping
    public ResponseEntity findTestPage () {

        List<PageVo> vos = testSoulService.showPaperList();

        return ResponseEntity.ok(vos);
    }


    /**
     * 问卷的提交
     * POST  /testSoul
     */
    @PostMapping
    public ResponseEntity putAnswers(@RequestBody Map map){
        /**
         * 和以往不一样   因为传递过来的参数是一个数组 Map<问题的id , 选项的id>
         * 而且用户作答的问题肯定不止一个  所有应该是一个集合
         * 所以最后的格式就是   List<Map<问题的id , 选项的id>>
         */
        List<Map<String,String>> answers = (List<Map<String, String>>) map.get("answers");
        String reportId = testSoulService.putAnswers(answers);
        return ResponseEntity.ok(reportId);
    }

    /**
     * 查询报告
     * GET  /testSoul/report/:id
     * @param id  报告的id
     * @return
     */
    @GetMapping("/report/{id}")
    public ResponseEntity getReport(@PathVariable("id") String id){
        ReportVo vo= testSoulService.getReport(id);
        return ResponseEntity.ok(vo);
    }

}
